DISCUSSION
The
ATSUPositionToOffset function obtains the edge offset(s) that correspond to the glyph nearest a mouse-down event. If the mouse-down event occurs at a line direction boundary, a second offset is passed back in the
oSecondaryOffset parameter representing the offset that corresponds to the furthest edge of the glyph beneath the hit point on the screen. A line direction boundary can occur on the trailing edges of two glyphs, the leading edges of two glyphs, or at the beginning or end of a text segment. The value passed back in the
oIsLeading parameter indicates the line direction of the text at the location of the mouse-down event.
The user expects that pressing the mouse button correlates to particular actions in an application. You can use the offset(s) passed back in
ATSUPositionToOffset for providing feedback or performing any actions in response to the user.
For example, if the user presses the mouse button in text, your application should pass the resulting edge offset to
ATSUOffsetToPosition to determine the caret position(s) corresponding to this offset. If the user presses the mouse button while the cursor is on a glyph and drags the cursor across a range of text, then releases the mouse, your application might want to respond by highlighting the text between the mouse-down and mouse-up events. To do this, you would pass the edge offset (s) passed back from
ATSUPositionToOffset that correspond to the mouse-up and mouse-down event positions to the
ATSUHighlightText function.
If the user then presses the mouse button outside the highlighted area, your application should pass the same edge offsets to the
ATSUUnhighlightText function. If the user double clicks (word selection) or triple clicks (paragraph selection), You can pass the resulting primary edge offset to
ATSUOffsetToPosition to determine the caret position(s) corresponding to this offset.
ATSUI does not keep actual line positions. As a result, the coordinates passed in the
iLocationX and
iLocationY parameters are relative to the position in the current graphics port of the origin of the line in which the mouse-down occurred. The passed back edge offset(s) are thus offsets from the beginning of the line in which the hit occurred, not from the beginning of the text layout object's buffer.
To transform the hit point's position, you must first call the
GlobalToLocal function, described in "Basic QuickDraw" in
Inside Macintosh: Imaging with QuickDraw
, to translate the global coordinates passed back in the
where field of the event record to local coordinates. For more information about responding to mouse-down events, see the "The Event Manager" in
Inside Macintosh: Macintosh Toolbox Essentials
. You then subtract the hit point (in local coordinates) from the position of the line's origin in the current graphics port.
For example, if you have a mouse-down event whose position in local coordinates is (75,50), you would subtract this value from the position of the origin of the line in the current graphics port. If the position of the origin of the line in the current graphics port is (50,50), then the relative position of the hit that you would pass in the
iLocationX and
iLocationY parameters would be (25,0).
ATSUPositionToOffset passes
back a
Boolean value in the
oIsLeading parameter to tell you the text direction of the primary edge offset. This directionality is determined by the Unicode directionality of the original character in backing-store memory. If it passes back
true, the primary edge offset is more closely associated with the following character in the backing-store memory. If it passes back
false, the primary offset is more closely associated with the previous character in the backing-store memory.
The following summarizes the possible outcomes of calling
ATSUPositionToOffset:
-
When the input pixel location (that is, the location of the hit point on the screen) is on the leading edge of the glyph, ATSUPositionToOffset passes back primary and secondary offsets corresponding to that glyph and an oIsLeading flag of true. If the glyph represents multiple characters and the style run attribute corresponding to the kATSUNoLigatureSplitTag has been set for them, ATSUPositionToOffset passes back an edge offset representing the beginning of this group of characters in memory.
-
When the input pixel location is on the trailing edge of the glyph, ATSUPositionToOffset passes back primary and secondary offsets representing the ending of this group of characters in memory following
the character or characters represented by the glyph and an oIsLeading flag of false.
-
When the input pixel location is beyond the leftmost or rightmost caret positions (not taking into account line rotation) such that no glyph corresponds to the location of the hit, ATSUPositionToOffset passes back the primary edge offset of the closest edge of the line to the input location. The oIsLeading flag depends on the directionality of the closest glyph and the side of the line the input location is closest to. In this case, the secondary offset is equal to the primary offset, since no glyph was hit.